define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'logistics/index' + location.search,
                    table: 'grade_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        /*{checkbox: true},*/
                        {field: 'id', title: __('Id')},
                        {field: 'orderid', title: __('Orderid'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'is_dk_name', title: '是否代缴(被交人手机号)'},
                        {field: 'amount', title: '现金', operate:'BETWEEN'},
                        {field: 'balance', title: '余额', operate:'BETWEEN'},
                        {field: 'gift.title', title: '礼品名称', operate: 'LIKE', formatter:function (value, row, index) {
                                if(row.gid==5){
                                    if(row.is_ka==1){
                                        return '(有卡)'+value;
                                    }else{
                                        return '(无卡)'+value;
                                    }
                                }else{
                                    return value;
                                }

                            }
                        },
                        /*{field: 'gid', title: '会员等级'},*/
                        {field: 'gift_status_name', title: '状态'},

                        {field: 'name', title: '联系人', operate: 'LIKE'},
                        {field: 'mobile', title: '手机号', operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'fahuotype_name', title: '发货方式'},
                        {field: 'express_name', title:'快递名称'},
                        {field: 'express_no', title:'快递单号'},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'TDG',
                                text: '发货',
                                // icon: 'fa fa-pencil',
                                classname: 'btn btn-xs btn-info btn-dialog',
                                url: 'logistics/fahuo'
                            }],
                            formatter: function(value, row, index){
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row.gift_status != 0 || row.fahuotype==1){
                                    $(table).data("operate-TDG", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        fahuo: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
